﻿using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System;
using System.IO;
using System.Windows;
using ZhuXingFamilys.Database;
using ZhuXingFamilys.Help;
using ZhuXingFamilys.MainViewModel;
using ZhuXingFamilys.User;
using ZhuXingFamilys.utils;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;

namespace ZhuXingFamilys.User
{
    [Transaction(TransactionMode.Manual)]
    public class UserLoginCommand : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UserLogin form1 = new UserLogin(UserName, UserPassword);
            UserCenter userCenter = new UserCenter();

            if (TestZKLogincommand() == false)
            {
                System.Windows.Interop.WindowInteropHelper mainUI = new System.Windows.Interop.WindowInteropHelper(form1);
                mainUI.Owner = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
                form1.ShowDialog();
            }
            else
            {
                System.Windows.Interop.WindowInteropHelper mainUI = new System.Windows.Interop.WindowInteropHelper(userCenter);
                mainUI.Owner = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
                userCenter.Show();
            };
            return Result.Succeeded;
        }
        static string iniFilePath = "LoginInfo.ini";
        static string CurAppDir = Set.getaddress();
        public const string CONFIG_LOGIN_CONTENT = "LoginInfo";
        public const string CONFIG_LOGIN_USERNAME_CONTENT = "UserName";
        public const string CONFIG_LOGIN_PASS_CONTENT = "UserPassword";
        public const string CONFIG_LOGIN_LOGINTIME_CONTENT = "LoginTime";
        public const string CONFIG_LOGIN_IsRemember_CONTENT = "IsRemember";
        public const string CONFIG_LOGIN_Authority_CONTENT = "Authority";
        public static string UserName;
        public static string UserPassword;
        public static string LoginTime;
        public static string IsRemember;
        public static string Authority;
        public static bool TestZKLogincommand()
        {
            DateTime NowTime = DateTime.Now;
            string time = "48";
            bool HasPermission = false;
            //配置文件路径是相对路径则需加上当前exe所在路径作为根路径
            if (!iniFilePath.Contains("\\") || !Path.IsPathRooted(iniFilePath))
            {
                iniFilePath = Path.Combine(CurAppDir, iniFilePath);
            }
            Set.GetValue(CONFIG_LOGIN_CONTENT, CONFIG_LOGIN_Authority_CONTENT, iniFilePath, out string Authority);
            // 权限
            if ("0".Equals(Authority) || "1".Equals(Authority))
            {
                return true;
            }
            return false;
        }

        internal static bool TestYanBanLogincommand()
        {
            string iniFilePath = "LoginInfo.ini";
            string CurAppDir = Set.getaddress();
            //配置文件路径是相对路径则需加上当前exe所在路径作为根路径
            if (!iniFilePath.Contains("\\") || !Path.IsPathRooted(iniFilePath))
            {
                iniFilePath = Path.Combine(CurAppDir, iniFilePath);
            }
            Set.GetValue(CONFIG_LOGIN_CONTENT, CONFIG_LOGIN_Authority_CONTENT, iniFilePath, out string Authority);
            // 权限
            if ("0".Equals(Authority) || "2".Equals(Authority))
            {
                return true;
            }
            return false;
        }
    }
}
